﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using HRProject.Code.DAO;
using HRProject.Code.DTO;
using System.Collections.Generic;
using System.IO;

namespace HRProject.Record
{
    public partial class AjaxRecord2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sAction = Request.Params["action"];
            string sRecordTypeId = Request.Params["recordTypeId"];
            //string sRecordTypeId = Request.Params["recordTypeId"];
            int updateResult = 0;
            int errorCode = 0;
            string status = "false";
            StringBuilder result = new StringBuilder();
            string error = string.Empty;

            try
            {
                Guid recordTypeId = new Guid(sRecordTypeId);

                RecordTypeDAO recordTypeDao = new RecordTypeDAO();
                BLRecordType filter = new BLRecordType();
                filter.Id = recordTypeId;
                if ("select_node".Equals(sAction))
                {
                    filter.Name = string.Empty;
                    List<BLRecordType> recordType = recordTypeDao.Search(filter);
                    StringBuilder s = new StringBuilder();
                    if (recordType.Count > 0)
                    {
                        AttachmentDAO attachmentDao = new AttachmentDAO();
                        BLAttachment fileFilter = new BLAttachment();
                        fileFilter.Id = Guid.Empty;
                        fileFilter.ContentId = recordTypeId;
                        List<BLAttachment> files = attachmentDao.Search(fileFilter);

                        s.Append("\"recordType\":{\"content\":\"");
                        s.Append(recordType[0].ContentForJSON);
                        s.Append("\",\"name\":\"");
                        s.Append(recordType[0].Name);
                        s.Append("\",\"files\":[");
                        int i = 0;
                        DownloadableFileDAO downloadDao = new DownloadableFileDAO();
                        BLDownloadableFile file = new BLDownloadableFile();
                        foreach (BLAttachment f in files)
                        {
                            if (i > 0)
                            {
                                s.Append(",");
                            }
                            file.FileName = f.Title;
                            file.Url = f.Url;
                            file.GeneratedName = Path.GetRandomFileName();
                            downloadDao.Add(file, ref errorCode);
                            s.Append("{\"id\":\"");
                            s.Append(f.Id);
                            s.Append("\",\"dfile\":\"");
                            s.Append(file.GeneratedName);
                            s.Append("\",\"title\":\"");
                            s.Append(f.Title);
                            s.Append("\",\"url\":\"");
                            s.Append(f.Url);
                            s.Append("\"}");
                            i++;
                        }
                        s.Append("]}");
                        
                        status = "true";
                    }
                    else
                    {
                        status = "false";
                    }

                    result.Append("{\"status\":");
                    result.Append(status);
                    result.Append(",");
                    result.Append(s.ToString());
                    result.Append(",\"error\":\"");
                    result.Append(error);
                    result.Append("\"}");
                }

                Response.Clear();
                Response.ContentType = "application/json; charset=utf-8";
                Response.Write(result);
                Response.End();
            }
            catch (Exception exp)
            {
                Console.Out.WriteLine(exp.Message);
            }
        }
    }
}
